package cn.cxion.chat.app.Fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.othershe.calendarview.bean.DateBean;
import com.othershe.calendarview.listener.OnPagerChangeListener;
import com.othershe.calendarview.listener.OnSingleChooseListener;
import com.othershe.calendarview.utils.CalendarUtil;
import com.othershe.calendarview.weiget.CalendarView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cxion.chat.R;
import cn.cxion.chat.app.adapter.ScheduleAdapter;
import cn.cxion.chat.app.bean.MessageAction;
import cn.cxion.chat.app.bean.MessageEvent;
import cn.cxion.chat.app.constant.Constant;
import cn.cxion.chat.app.util.LogUtil;

public class CaledarFragment2 extends Fragment {


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.calendar)
    CalendarView calendarView;
    @Bind(R.id.choose_date)
    TextView chooseDate;
    @Bind(R.id.activity_main)
    LinearLayout activityMain;
    @Bind(R.id.lastMonth)
    ImageView lastMonth;
    @Bind(R.id.nextMonth)
    ImageView nextMonth;
    @Bind(R.id.today)
    TextView today;
    @Bind(R.id.someday)
    TextView someday;
    @Bind(R.id.start)
    TextView start;
    @Bind(R.id.end)
    TextView end;
    @Bind(R.id.lastYear)
    TextView lastYear;
    @Bind(R.id.nextYear)
    TextView nextYear;
    @Bind(R.id.multiChoose)
    TextView multiChoose;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    ScheduleAdapter adapter;

    private int[] cDate = CalendarUtil.getCurrentDate();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_caledar2, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        init();
        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(MessageEvent messageEvent){
        if(messageEvent.getKey()== MessageAction.ReSchedule){
            adapter.setList(Constant.getScheduleBeanList());
            adapter.notifyDataSetChanged();
            LogUtil.print("刷新");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private void init() {
        initView();
        initListView();
    }

    private void initListView(){
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter=new ScheduleAdapter(getActivity());
        adapter.setList(Constant.getScheduleBeanList());
        recyclerView.setAdapter(adapter);

    }


    private void initView() {
        //当前选中的日期

        HashMap<String, String> map = new HashMap<>();
        map.put("2019.10.30", "qaz");
        map.put("2019.10.1", "wsx");
        map.put("2019.11.12", "yhn");
        map.put("2019.9.15", "edc");
        map.put("2019.11.6", "rfv");
        map.put("2019.11.11", "tgb");
        calendarView
//                .setSpecifyMap(map)
                .setStartEndDate("2018.1", "2050.12")
                .setDisableStartEndDate("2018.1.1", "2050.12.30")
                .setInitDate(cDate[0] + "." + cDate[1])
                .setSingleDate(cDate[0] + "." + cDate[1] + "." + cDate[2])
                .init();
        title.setText(cDate[0] + "年" + cDate[1] + "月");
        chooseDate.setText("" + cDate[0] + "年" + cDate[1] + "月" + cDate[2] + "日");

        calendarView.setOnPagerChangeListener(new OnPagerChangeListener() {
            @Override
            public void onPagerChanged(int[] date) {
                title.setText(date[0] + "年" + date[1] + "月");
            }
        });

        calendarView.setOnSingleChooseListener(new OnSingleChooseListener() {
            @Override
            public void onSingleChoose(View view, DateBean date) {
                title.setText(date.getSolar()[0] + "年" + date.getSolar()[1] + "月");
                if (date.getType() == 1) {
                    chooseDate.setText("" + date.getSolar()[0] + "年" + date.getSolar()[1] + "月" + date.getSolar()[2] + "日 ");
                }
            }
        });
    }

    public void someday(View v) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.input_layout, null);
        final EditText year = (EditText) view.findViewById(R.id.year);
        final EditText month = (EditText) view.findViewById(R.id.month);
        final EditText day = (EditText) view.findViewById(R.id.day);

        new AlertDialog.Builder(getActivity())
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (TextUtils.isEmpty(year.getText())
                                || TextUtils.isEmpty(month.getText())
                                || TextUtils.isEmpty(day.getText())) {
                            Toast.makeText(getActivity(), "请完善日期！", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        boolean result = calendarView.toSpecifyDate(Integer.valueOf(year.getText().toString()),
                                Integer.valueOf(month.getText().toString()),
                                Integer.valueOf(day.getText().toString()));
                        if (!result) {
                            Toast.makeText(getActivity(), "日期越界！", Toast.LENGTH_SHORT).show();
                        } else {
                            chooseDate.setText("" + year.getText() + "年" + month.getText() + "月" + day.getText() + "日");
                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", null).show();
    }

    @OnClick({R.id.lastMonth, R.id.nextMonth, R.id.today, R.id.someday, R.id.start, R.id.end, R.id.lastYear, R.id.nextYear, R.id.multiChoose})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lastMonth:
                calendarView.lastMonth();
                break;
            case R.id.nextMonth:
                calendarView.nextMonth();
                break;
            case R.id.today:
                calendarView.today();
                chooseDate.setText("" + cDate[0] + "年" + cDate[1] + "月" + cDate[2] + "日");
                break;
            case R.id.someday:
                break;
            case R.id.start:
                break;
            case R.id.end:
                break;
            case R.id.lastYear:
                break;
            case R.id.nextYear:
                break;
            case R.id.multiChoose:
                break;
        }
    }
}
